package com.ruoyi.workbench.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.workbench.domain.WorkTask;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workbench.domain.WorkTargetTask;
import com.ruoyi.workbench.service.IWorkTargetTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分解任务Controller
 * 
 * @author Guojl
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/targetTask/targetTask")
public class WorkTargetTaskController extends BaseController
{
    @Autowired
    private IWorkTargetTaskService workTargetTaskService;

    /**
     * 查询分解任务列表
     */
    @PreAuthorize("@ss.hasPermi('targetTask:targetTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkTargetTask workTargetTask)
    {
        startPage();
        List<WorkTargetTask> list = workTargetTaskService.selectWorkTargetTaskList(workTargetTask);
        return getDataTable(list);
    }

    /**
     * 查询分解任务列表--工作任务清单使用，用于合并表格
     */
    @GetMapping("/listForItem")
    public TableDataInfo listForItem(WorkTargetTask workTargetTask)
    {
        startPage();
        return workTargetTaskService.selectWorkTargetTaskForItemList(workTargetTask);
    }

    /**
     * 导出分解任务列表
     */
    @Log(title = "分解任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkTargetTask workTargetTask)
    {
        List<WorkTargetTask> list = workTargetTaskService.selectWorkTargetTaskList(workTargetTask);
        ExcelUtil<WorkTargetTask> util = new ExcelUtil<WorkTargetTask>(WorkTargetTask.class);
        util.exportExcel(response, list, "分解任务数据");
    }

    /**
     * 获取分解任务详细信息
     */
    @GetMapping(value = "/{targetTaskId}")
    public AjaxResult getInfo(@PathVariable("targetTaskId") Long targetTaskId)
    {
        return success(workTargetTaskService.selectWorkTargetTaskByTargetTaskId(targetTaskId));
    }

    /**
     * 新增分解任务
     */
//    @Log(title = "分解任务", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody WorkTargetTask workTargetTask)
//    {
//        return toAjax(workTargetTaskService.insertWorkTargetTask(workTargetTask));
//    }

    /**
     * 新增分解任务
     */
    @Log(title = "分解任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<WorkTargetTask> workTargetTasks)
    {
        return toAjax(workTargetTaskService.insertWorkTargetTask(workTargetTasks));
    }

    /**
     * 修改分解任务
     */
    @Log(title = "分解任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkTargetTask workTargetTask)
    {
        return toAjax(workTargetTaskService.updateWorkTargetTask(workTargetTask));
    }
    /**
     * 修改分解任务
     */
    @Log(title = "分解任务", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/updateById")
    public AjaxResult updateById(@RequestBody WorkTargetTask workTargetTask)
    {
        return toAjax(workTargetTaskService.updateWorkTargetTaskById(workTargetTask));
    }

    /**
     * 删除分解任务
     */
    @Log(title = "分解任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{targetTaskIds}")
    public AjaxResult remove(@PathVariable Long[] targetTaskIds)
    {
        return toAjax(workTargetTaskService.deleteWorkTargetTaskByTargetTaskIds(targetTaskIds));
    }

    /**
     * 查询工作任务列表
     */
    @PreAuthorize("@ss.hasPermi('targetTask:targetTask:list')")
    @GetMapping("/getTargetTaskList")
    public AjaxResult getList(WorkTargetTask workTargetTask)
    {
        List<WorkTargetTask> list = workTargetTaskService.selectWorkTargetTaskList(workTargetTask);
        return AjaxResult.success(list);
    }
}
